package com.browsesoft.resource;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.EntityManager;
import com.browsesoft.ManagerGroup;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLFormModel;

public class UniversalResourceEntity extends BasicResource implements
		HTMLFormModel, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2618884696544189476L;

	/**
	 * 
	 */
	protected String tableName = null;

	protected String resourceType = null;

	/**
	 * 空构造
	 */
	public UniversalResourceEntity() {
		super();
	}

	/**
	 * 初始化实体
	 */
	public void init(HttpServletRequest request) throws Exception {
		super.init(request);
		// 接受模板名称
		this.templateName = request.getParameter("templatename");
		// 设置模板
		this.setTemplateName(templateName);
		String id = request.getParameter("point");
		if (id != null && !id.equals("")) {
			// 从实体管理器中取得属性
			this.attributes = ((Resource) EntityManager.getInstance().getObject(id))
					.getAttributes();
		}
	}

	/**
	 * 使用属性构造
	 */
	public UniversalResourceEntity(Hashtable attrs) {
		super(attrs);
	}

	/**
	 * 设置用户属性
	 * 
	 */
	public void updateAttributes(Hashtable attrs) throws Exception {
		tableName = this.getComponent().getElement().getAttribute("tablename");
		this.attributes.put("tablename", tableName);
		// 如果应该插入
		if (isShouldInsert()) {
			// 放入资源类型
			this.attributes.put("resourcetype", this.resourceType);
			// 得到资源对应的图片
			String icon = this.getComponent().getElement().getAttribute("icon");
			// 如果有
			if (icon != null && !icon.equals("")) {
				this.attributes.put("f_icon", icon);
			}
			super.updateAttributes(attrs);
			// 更新父资源
			parent.update();
			parentID = null;
		} else {
			super.updateAttributes(attrs);
		}
	}

	/**
	 * 记录客户端环境
	 * 
	 * @param request
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.service(request, response);
		this.resourceType = (String) this.getComponent().getElement().getAttribute(
				"resourcetype");
	}

	/**
	 * 取得选项值列表
	 */
	public Iterator getOptions(String selectName) {
		return null;
	}

	/**
	 * 模型更新页面元素
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	public LinkedList getRoot() {
		return null;
	}

	public LinkedList getfirstResourceByType(String parenttype, String childtype)
			throws Exception {
		// 取得实体管理器
		EntityManager entitymanager = (EntityManager) ManagerGroup
				.getManagerGroup().getManager("entity");
		LinkedList templist = new LinkedList();
		// 根据parentid取得资源对象
		Resource parent = this.getParent();
		if (this.parentID != null && parent == null) {
			parent = (Resource) entitymanager.getObject(parentID);
		}
		while (!parent.getType().equals(parenttype)) {
			parent = parent.getParent();
		}
		Iterator list = parent.getChildren();
		while(list.hasNext()) {
			Resource resource = (Resource) list.next();
			if (resource.getType().equals(childtype)) {
				templist.add(resource);
			}
		}
		return templist;
	}

	public String getTableName() {
		return (String) this.attributes.get("tablename");
	}
}